//
//  MoreView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/30.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

public protocol MoreHeaderViewDelegate {
    
    // 出发城市点击
    func search_StartCity()
    
    // 到达城市点击
    func search_stopCity()
    
    // 切换城市
    func search_switchCity()
}

class MoreHeaderView: UIView {
    
    // 航班号标题
    private lazy var startCityTitle = UILabel(title: "出发城市",color: COLOR_NICE,size: 12)
    // 出发城市选择
    public lazy var cityView = InputButtonView()
    
    // 到达城市选择
    public lazy var endcityView = InputButtonView()
    // 到达城市标题
    private lazy var stopCityTitle = UILabel(title: "到达城市",color: COLOR_NICE,size: 12)
    
    // 切换城市
    private lazy var switchCity: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "flight_change_17x17"), for: .normal)
        return btn
    }()
    
    // 线宽
    private let lineWidth = (SCREEN_WIDTH - MARGIN * 2) * 0.39
    // 屏宽
    private let screenWidth = (SCREEN_WIDTH - MARGIN * 2) * 0.5
    
    // MARK: 代理
    public var delegate:MoreHeaderViewDelegate?
    
    public var resetStopCity:String? {
        didSet {
            endcityView.setTitle(resetStopCity, for: .normal)
        }
    }
    
    public var resetStartCity:String? {
        didSet {
            cityView.setTitle(resetStartCity, for: .normal)
        }
    }
    
    override func layoutSubviews() {
        // 出发城市标题
        startCityTitle.frame = CGRect(x: MARGIN, y: MARGIN20, width: MARGIN * 4, height: MARGIN)
        addSubview(startCityTitle)
        // 出发城市选择
        cityView.setTitle("北京", for: .normal)
        cityView.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        cityView.frame = CGRect(x: MARGIN, y: startCityTitle.frame.maxY, width: lineWidth, height: MARGIN50)
        addSubview(cityView)
        // 出发城市 添加监听事件
        cityView.addTarget(self, action: #selector(clickStartCity), for: .touchUpInside)
        
        // 切换图片
        switchCity.frame = CGRect(x: center.x - MARGIN30, y: startCityTitle.frame.maxY + MARGIN10, width: MARGIN30, height: MARGIN30)
        addSubview(switchCity)
        // 切换图片 添加监听事件
        switchCity.addTarget(self, action: #selector(clickSwitchCity), for: .touchUpInside)
        
        // 到达城市标题
        stopCityTitle.textAlignment = .right
        let stopX = frame.width - screenWidth - MARGIN
        stopCityTitle.frame = CGRect(x:stopX, y: MARGIN20, width: screenWidth, height: MARGIN)
        addSubview(stopCityTitle)
        // 到达城市选择
        endcityView.alignment = .right
        endcityView.setTitle("上海", for: .normal)
        endcityView.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
        let stopCityX = frame.width - lineWidth - MARGIN
        endcityView.frame = CGRect(x: stopCityX, y: startCityTitle.frame.maxY, width: lineWidth, height: MARGIN50)
        addSubview(endcityView)
        // 到达城市 添加监听事件
        endcityView.addTarget(self, action: #selector(clickStopCity), for: .touchUpInside)
    }
    
}

extension MoreHeaderView {
    
    // 切换城市点击
    @objc func clickSwitchCity() -> Void {
        guard let start = cityView.titleLabel?.text else {
            return
        }
        guard let stop = endcityView.titleLabel?.text else {
            return
        }
        endcityView.setTitle(start, for: .normal)
        cityView.setTitle(stop, for: .normal)
        // 切换
        delegate?.search_switchCity()
    }
    
    // 出发城市点击
    @objc func clickStartCity() -> Void {
        delegate?.search_StartCity()
    }
    
    // 到达城市点击
    @objc func clickStopCity() -> Void {
        delegate?.search_stopCity()
    }
}
